/**
 * PageMarket
 * ReportActivity.java
 * com.android.pagemarket.ui.activity.setting
 * 
 */
package com.android.pagemarket.ui.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.ReportAction;
import com.android.pagemarket.io.protocol.out.HttpResult;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-20
 * @see : 留言视图
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class ReportActivity extends TemplateActivity implements OnClickListener
{

    private EditText tx_name;
    private EditText tx_phone;
    private EditText tx_msg;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_report);
        initViews();
    }
    
    private void initViews()
    {
        tx_name = getViewById(R.id.tx_name);
        tx_phone = getViewById(R.id.tx_phone);
        tx_msg = getViewById(R.id.tx_msg);
        
        if(InitManager.getInstance().isLogin())
        {
            tx_name.setText(InitManager.getInstance().mLoginCompany.companyName);
        }
    }

    @Override
    public void onClick(View v)
    {
        String name = tx_name.getEditableText().toString().trim();
        String phone = tx_phone.getEditableText().toString().trim();
        String content = tx_msg.getEditableText().toString().trim();
        
        if(name.length() == 0 || !Util.isValidName(name))
        {
            showToast(getString(R.string.tip_name_check));
            return;
        }
        if(phone.length() == 0 || !Util.isPhoneNumberValid(phone))
        {
            showToast(getString(R.string.tip_phone_check));
            return;
        }
        if(content.length() == 0)
        {
            showToast(getString(R.string.tip_report_nul));
            return;
        }
        
        ReportAction action = new ReportAction(name, phone, content);
        action.setActionListener(new ActionListener<HttpResult>()
        {
            @Override
            public void onSucceed(HttpResult result)
            {
//                showLoading(false);
                hideLoding();
                if(result.isNoError())
                {
                    showToast(R.string.tip_report_ok);
                    tx_msg.setText("");
                }else
                {
                    showToast(result.resultInfo);
                }
            }

            @Override
            public void onError(int resultCode)
            {
//                showLoading(false);
                hideLoding();
                showToast(ReportAction.getErrorMean(resultCode));
            }
        });
//        showLoading(true);
        showLoding(null, "提交中...");
        action.submit();
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, ReportActivity.class);
        
        context.startActivity(it);
    }
}
